package com.woniuxy.sddfp.sms.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.woniuxy.sddfp.sms.config.SmsTemplate;
import com.woniuxy.sddfp.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * @author yellow Doctor
 * @date 2025-05-07
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String sendCode(String phone) {

        //01 生成验证码
        String code = RandomUtil.randomNumbers(4);

        //02 发送短信
//        smsTemplate.sendSms(phone,code);
        // 短信验证码 计时

        String codeKey = "user:" + phone + ":" + code;
        stringRedisTemplate.opsForValue().set(
                codeKey,
                code,
                Duration.ofMinutes(5) //ttl 5min
        );

        return "短信验证码是:" + code;
    }
}
